﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Eventhere.Work;
using System.IO.IsolatedStorage;
using System.Windows.Media;

namespace Eventhere.Controls
{
    public partial class ControlAjoutContact : PhoneApplicationPage
    {

        TypesAdd typeAjout;
        Repertory repertoire;

        public Repertory Repertoire
        {
            get { return repertoire; }
            set { repertoire = value; }
        }

        public enum TypesAdd
        {
            Repertory, Event, GroupContact
        }
        public TypesAdd TypeAjout
        {
            get { return typeAjout; }
            set { typeAjout = value; }
        }


        public ControlAjoutContact()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
                this.repertoire = (Repertory)IsolatedStorageSettings.ApplicationSettings["Repertoire"];

            this.Repertoire.addContact(new Contact("colin.rimbaud@gmail.com"));
            InitializeComponent();


            #region ajoutcontrolboutonparcontact
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            
            foreach (GroupContact g in this.repertoire.Groupes)// ON AFFICHE DABORD LES GROUPES
            {

                Button Unbutton = new Button();
                Unbutton.Content = g.NameGroupContact;
                Unbutton.FontWeight = FontWeights.Bold;
                Unbutton.Name = g.IdGroup.ToString();
                Unbutton.Background = new SolidColorBrush(Colors.DarkGray);
                if (g == this.repertoire.Groupes.First())
                    Unbutton.Margin = new Thickness(0, 0, 0, 0);
                else
                    Unbutton.Margin = new Thickness(0, -25, 0, 0);

                if (darkBackgroundVisibility == Visibility.Visible)
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.White);
                else
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.Black);
                Unbutton.Height = 100;
                Unbutton.BorderThickness = new Thickness(1);
                Unbutton.Padding = new Thickness(10, 0, 0, 0);
                Unbutton.UseLayoutRounding = true;
                Unbutton.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                Unbutton.Click += Button_Click_1;// Rootage vers la page RepertoryModel.xaml
                this.ContentRepertoryPanel.Children.Add(Unbutton);//Ajout du bouton au conteneur 
            }

            foreach (Contact c in this.repertoire.Contacts)// ENSUITE LES CONTACTS
            {


                Button Unbutton = new Button();
                Unbutton.Content = "NomContact";
                Unbutton.Name = c.IdContact.ToString();
                Unbutton.Margin = new Thickness(0, -25, 0, 0);
                if (darkBackgroundVisibility == Visibility.Visible) // theme sombre
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.White);
                else
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.Black); //theme clair
                Unbutton.Height = 100;
                Unbutton.BorderThickness = new Thickness(1);
                Unbutton.Padding = new Thickness(10, 0, 0, 0);
                Unbutton.UseLayoutRounding = true;
                Unbutton.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                Unbutton.Click += Button_Click_2;
                
                this.ContentRepertoryPanel.Children.Add(Unbutton);
            }
            #endregion

        }

        
        private void Button_Click_1(object sender, RoutedEventArgs e)//pour les groupcontact
        {
            switch (TypeAjout)
            {
                case TypesAdd.Event:
                    List<Event> events = (List<Event>)IsolatedStorageSettings.ApplicationSettings["Events"];
                    Event CurrentEvent = events.Last();
                    int id = int.Parse(((Button)sender).Name);
                    //Recupere la list de contact du group et l'ajouter dans levenement

                    
                    break;
                case TypesAdd.GroupContact:
                    //
                    break;
                case TypesAdd.Repertory:
                    //
                    break;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //meme que Button_Click_1 mais avec des contact
        }

    }
}